import storage from 'store'
// import { login, getInfo, logout } from '@/api/login'
import { getInfo, login } from '@/api/login'
import { ACCESS_TOKEN } from '@/store/mutation-types'
import { welcome } from '@/utils/util'

const user = {
  state: {
    token: '',
    name: '',
    welcome: '',
    avatar: '',
    roles: [],
    info: {}
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, { name, welcome }) => {
      state.name = name
      state.welcome = welcome
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_INFO: (state, info) => {
      state.info = info
    }
  },

  actions: {
    // 登录
    Login ({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        login(userInfo).then(response => {
          // const result = response.result
          storage.set(ACCESS_TOKEN, response.token, response.duration)

          commit('SET_TOKEN', response.token)
          // storage.set(ACCESS_TOKEN, response.access_token, response.expires_in)
          // commit('SET_TOKEN', response.access_token)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetInfo ({ commit }) {
      return new Promise((resolve, reject) => {
        getInfo(storage.get(ACCESS_TOKEN)).then(response => {
          const result = response
          if (result.role && result.role.permissions.length > 0) {
            const role = result.role
            role.permissions = result.role.permissions
            role.permissions.map(per => {
              if (per.actionEntitySet != null && per.actionEntitySet.length > 0) {
                per.actionList = per.actionEntitySet.map(action => {
                  return action.action
                })
              }
            })
            role.permissionList = role.permissions.map(permission => { return permission.permissionId })
            commit('SET_ROLES', result.role)
            commit('SET_INFO', result)
          } else {
            // reject(new Error('getInfo: roles must be a non-null array !'))
          }
          // TODO 临时指定角色
          result.role = {
            'id': 'admin',
            'name': '管理员',
            permissionList: ['dashboard', 'form', 'table', 'profile', 'support', 'user', 'admin']
          }
          commit('SET_ROLES', result.role)
          commit('SET_INFO', result)

          commit('SET_NAME', { name: result.nickname || result.name, welcome: welcome() })
          commit('SET_AVATAR', result.avatar)

          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 登出
    Logout ({ commit, state }) {
      return new Promise((resolve) => {
        commit('SET_TOKEN', '')
        commit('SET_ROLES', [])
        storage.remove(ACCESS_TOKEN)
        location.reload()
        // logout(state.token).then(() => {
        //   resolve()
        // }).catch(() => {
        //   resolve()
        // }).finally(() => {
        //   commit('SET_TOKEN', '')
        //   commit('SET_ROLES', [])
        //   storage.remove(ACCESS_TOKEN)
        // })
      })
    }

  }
}

export default user
